package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesGroup;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesGroupRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;

/**
 * 老师 ---备授课--授课报告-测验报告
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassTestReportController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingTestQuesGroupRepository yxyEsbagTeachingTestQuesGroupRepository;

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/class/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        String classId = reqModel.classId;
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        List<YxyEsbagTeachingTestQuesGroup> yxyEsbagTeachingTestQuesGroup = yxyEsbagTeachingTestQuesGroupRepository
                .findByTeacherIdAndSchoolIdAndClassIdAndPackIdOrderByCreatedDate(userId, schoolId, classId, id);

        List<RespModel> respModels = new ArrayList<RespModel>();
        for (YxyEsbagTeachingTestQuesGroup yxyEsbagTeachingTestQuesGroup2 : yxyEsbagTeachingTestQuesGroup) {
            RespModel model = new RespModel();
            model.quesGroupId = yxyEsbagTeachingTestQuesGroup2.getQuesGroupId();
            model.quesGroupImage = yxyEsbagTeachingTestQuesGroup2.getQuesGroupImage();
            model.quesGroupName = yxyEsbagTeachingTestQuesGroup2.getQuesGroupName();
            YxyLectureRes resourceId = yxyLectureResRepository
                    .findByResourceId(yxyEsbagTeachingTestQuesGroup2.getQuesGroupId());
            if (null != resourceId) {
                model.quesNum = resourceId.getQuesIds().size();
            }
            respModels.add(model);
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 授课包id
        public String id;
        // 班级id
        public String classId;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 题组logo
        public String quesGroupImage;
        // 题组ID
        public String quesGroupId;
        // 题组名称
        public String quesGroupName;
        // 试题数量
        public Integer quesNum;
    }
}
